import Vue from 'vue'
import Vuex from 'vuex'
import apis from '../../http/apis.js'
Vue.use(Vuex)

export default {
    namespaced: true,
    state: {
        pageData: {
            total: 10,
            pages: 0,
            pageSize: 5,
            currentPage: 1,
            claims: [{}]
        },
        addForm: {
            name: "",
            price: "",
            delivery: false, //立即创建
            type: [],
            resource: "",
        },
        editData: {
            name: "",
            accidentTime: "",
            applyTime: "",
            price: "",
            state: ""
            // delivery: false, //立即创建
            // type: [],
            // resource: "",
        },
    },
    mutations: {
        changePageData(state, pageData) {
            // console.log('changePageData', state, pageData);
            state.pageData.claims = pageData.data
        },
        // 理赔列表点击编辑（修改）渲染到修改界面的值
        edit(state, updateData) {
            state.editData = updateData
        },
        changePageSize(state, pageSize) {
            state.pageData.pageSize = pageSize
            // console.log('changePageSize', state.pageData.pageSize);
        },
        changeCurrentPage(state, currentPage) {
            state.pageData.currentPage = currentPage
            // console.log('CurrentPage', state.pageData.currentPage);
        }
    },

    actions: {
        // 获取
        async getClaims(context, state) {
            const {
                data
            } = await apis.claims.getClaims(state);
            // console.log("data", data.data);
            if (data.status) {
                context.commit('changePageData', data.data)
            }
        },


        // 新增
        async addClaims(context, state) {
            const data = await apis.claims.addClaims(state);
            if (data.status) {
                alert("新增理赔成功")
                location.hash = '#/adminsSystem/claimsList'
            } else {
                alert("新增理赔失败")
            }
        },
        //   删除
        async deleteClaims(context, row) {
            const data = await apis.claims.deleteClaims(row._id);
            if (data.status) {
                alert("删除理赔成功")
                context.dispatch('getClaims', context.state.pageData)
            } else {
                alert("删除理赔失败")
            }
        },
        // 通过id获取
        async getClaimsById(context, _id) {
            const {
                data
            } = await apis.claims.getClaimsById(_id);
            // console.log(data.data[0]);
            if (data.status) {
                // console.log(context.state.updateData);
                context.commit('edit', data.data[0])
            }
        },

        //  确认修改
        async updateClaims(context, row) {
            const data = await apis.claims.updateClaims(row);
            console.log(data);
            if (data.status) {
                alert("修改学生成功")
                location.hash = '#/adminsSystem/claimsList'
            } else {
                alert("修改理赔失败")
            }
        },

    },


    modules: {}
}